package com.gxa.modules.service.drivingManagementService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.drivingManagement.MealPriceDTO;
import com.gxa.modules.dto.drivingManagement.QueryTrainMealDTO;
import com.gxa.modules.dto.drivingManagement.TrainMealDTO;
import com.gxa.modules.entity.TrainMeal;
import com.gxa.modules.mapper.drivingManagement.TrainMealMapper;
import com.gxa.modules.service.drivingManagementService.TrainMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class TrainMealServiceImpl implements TrainMealService {

    @Autowired
    private TrainMealMapper trainMealMapper;
    @Override
    public RespResult addTrainMeal(TrainMeal trainMeal) {//添加培训套餐
        int result = trainMealMapper.insert(trainMeal);
        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("添加失败！");
        }
    }

    @Override
    public RespResult showMealPriceInfo() {//显示套餐价格详情
        QueryWrapper<TrainMeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("train_price","platform_price","fingerprint","make_price","one_price","two_price","three_price","four_price");

        TrainMeal trainMeal = trainMealMapper.selectOne(queryWrapper);


        return RespResult.success(trainMeal);
    }

    @Override
    public RespResult queryAllTrainMeal(QueryTrainMealDTO queryTrainMealDTO) {//查询全部培训套餐
        Page<TrainMeal> trainMealPage = new Page<>(queryTrainMealDTO.getPage(),queryTrainMealDTO.getLimit());

        QueryWrapper<TrainMeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(queryTrainMealDTO.getMealName()),"meal_name",queryTrainMealDTO.getMealName());

        try{
            Page<TrainMeal> queryTrainMealDTOList = trainMealMapper.selectPage(trainMealPage,queryWrapper);
            return RespResult.success(queryTrainMealDTOList);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult queryTrainMealByName(String mealName) {//根据套餐名称查询培训套餐信息

        QueryWrapper<TrainMeal> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("meal_name" ,mealName);

        try{
            TrainMeal trainMeal = trainMealMapper.selectOne(queryWrapper);
            return RespResult.success(trainMeal);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult updateTrainMeal(TrainMeal trainMeal) {//更新培训套餐

        int result = trainMealMapper.updateById(trainMeal);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("更新失败！");
        }
    }

}
